package cj.com.chabaike.ui.act;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.viewpagerindicator.IconPagerAdapter;
import com.viewpagerindicator.LinePageIndicator;

import cj.com.chabaike.R;
import cj.com.chabaike.ui.frag.ExplainFragment;
import cj.com.chabaike.ui.frag.ExplainLastFragment;

public class ExplainPager extends FragmentActivity {

    ViewPager viewPager;
    //	LinearLayout dotCont;
    LinePageIndicator mIndicator;
    List<Fragment> fragList;
    ExplainAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.explainpager);
        viewPager = (ViewPager) findViewById(R.id.explainViewPager);
//		dotCont = (LinearLayout) findViewById(R.id.explainDotCont);
        initViewPager();

    }


    private void initViewPager() {
        // TODO Auto-generated method stub
        ExplainFragment frag1 = new ExplainFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("resId", R.drawable.wel_pager1);
        frag1.setArguments(bundle);

        Bundle bundle2 = new Bundle();
        bundle2.putInt("resId", R.drawable.wel_pager2);
        ExplainFragment frag2 = new ExplainFragment();
        frag2.setArguments(bundle2);


        ExplainLastFragment frag3 = new ExplainLastFragment();
        fragList = new ArrayList<>();
        fragList.add(frag1);
        fragList.add(frag2);
        fragList.add(frag3);

        adapter=new ExplainAdapter(getSupportFragmentManager());

        viewPager.setAdapter(adapter);
//        viewPager.setAdapter(new FragmentPagerAdapter(
//                getSupportFragmentManager()) {
//
//            @Override
//            public int getCount() {
//                // TODO Auto-generated method stub
//                return fragList.size();
//            }
//
//            @Override
//            public Fragment getItem(int arg0) {
//                // TODO Auto-generated method stub
//                return fragList.get(arg0);
//            }
//        });

        mIndicator = (LinePageIndicator) findViewById(R.id.indicator);
        mIndicator.setViewPager(viewPager);

//			final ImageView image1 = new ImageView(this);
//			image1.setBackgroundResource(R.drawable.dot);
//			final ImageView image2 = new ImageView(this);
//			image2.setBackgroundResource(R.drawable.dot_1);
//			final ImageView image3 = new ImageView(this);
//			image3.setBackgroundResource(R.drawable.dot_1);
//			dotCont.addView(image1);
//			dotCont.addView(image2);
//			dotCont.addView(image3);

//        viewPager.setOnPageChangeListener(new OnPageChangeListener() {
//
//            @Override
//            public void onPageSelected(int arg0) {
//                // TODO Auto-generated method stub
//                    /*switch (arg0) {
//					case 0:
//						image1.setBackgroundResource(R.drawable.dot);
//						image2.setBackgroundResource(R.drawable.dot_1);
//						image3.setBackgroundResource(R.drawable.dot_1);
//						break;
//					case 1:
//						image2.setBackgroundResource(R.drawable.dot);
//						image1.setBackgroundResource(R.drawable.dot_1);
//						image3.setBackgroundResource(R.drawable.dot_1);
//						break;
//					case 2:
//						image3.setBackgroundResource(R.drawable.dot);
//						image1.setBackgroundResource(R.drawable.dot_1);
//						image2.setBackgroundResource(R.drawable.dot_1);
//						break;
//
//					default:
//						break;
//					}*/
//
//            }
//
//            @Override
//            public void onPageScrolled(int arg0, float arg1, int arg2) {
//                // TODO Auto-generated method stub
//
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int arg0) {
//                // TODO Auto-generated method stub
//
//            }
//        });
/*
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                    /*switch (arg0) {
					case 0:
						image1.setBackgroundResource(R.drawable.dot);
						image2.setBackgroundResource(R.drawable.dot_1);
						image3.setBackgroundResource(R.drawable.dot_1);
						break;
					case 1:
						image2.setBackgroundResource(R.drawable.dot);
						image1.setBackgroundResource(R.drawable.dot_1);
						image3.setBackgroundResource(R.drawable.dot_1);
						break;
					case 2:
						image3.setBackgroundResource(R.drawable.dot);
						image1.setBackgroundResource(R.drawable.dot_1);
						image2.setBackgroundResource(R.drawable.dot_1);
						break;

					default:
						break;
					}

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

        */
    }
    class ExplainAdapter extends FragmentPagerAdapter implements IconPagerAdapter{

        public ExplainAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragList.get(position);
        }

        @Override
        public int getIconResId(int index) {
            return index;
        }

        @Override
        public int getCount() {
            return fragList.size();
        }
    }

}
